package com.ludomancy.timescape;

import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.Iterator;

import com.ludomancy.gamelib.Widget;

public class LandscapeWidget extends Widget
{

	public LandscapeWidget(Widget parent, Rectangle area)
	{
		super(parent, area);
	}

	public void mouseDown(Point pos)
	{
		Point tilePos = getTileForPos(pos);
		if(Game.game.isWithin(tilePos))
		{
			Entity e= Game.game.getEntityUnder(tilePos);
			if(e == null)
			{
				// add plant
				Game.game.addEntity(new WaterEntity(Game.game.currentTime, tilePos.x, tilePos.y), true);
			}
			else
			{
				Game.game.removeEntity(e);
			}
		}
	}
	
	private Point getTileForPos(Point pos)
	{
		return new Point(pos.x / Game.TILE_SIZE, pos.y / Game.TILE_SIZE);
	}
	
	public void paint(Graphics g)
	{
		// paint terrain
		Game.game.currentTerrain.paint(g);
		// paint entities
		for (Iterator iter = Game.game.entities.iterator(); iter.hasNext();)
		{
			Entity entity = (Entity) iter.next();
			if(entity.initialTime <= Game.game.currentTime)
			{
				entity.paint(g);
			}
		}
	}

}
